<?php
namespace AuctionCore\Spm;
use AuctionCore\Spm\AccountManage;
/**
 * 车源列表
 * @author       shijiabing
 */
class DealStat extends \AuctionSpm\Controller\BaseController {
    //初始化
    public function __construct() {           
        parent::__construct();
    }
	private $_cols = "c.car_id,c.car_no,c.car_status,c.suggest_min_price,c.suggest_max_price
                ,c.re_auction_reason,c.success_price,o.order_no,o.order_id,o.status as ostatus,o.trade_time,co.owner_id,co.status,co.check_date,co.business_status";

	public function get($ev_page_num, $page) {
        
    }
    public function count(){

    }
    /**
     * 计算参拍量和成交量
     * @param  array $map 查询条件
     * @return [type]      [description]
     */
    public function counts($map) {

        // $user_m = new AccountManage();
        // $map1 = $user_m->getFourMap();
        // $map = array_merge($map,$map1);

        $map['c.suggest_max_price'] = array('neq',' ');
        $map['c.suggest_min_price'] = array('neq',' ');
        $count = M('cars')->alias('c')->field($this->_cols)
        			->join("LEFT JOIN " . C('DB_PREFIX') . "order o ON c.car_id=o.car_id")
	                ->join("LEFT JOIN ".C('DB_PREFIX')."car_owner as co ON co.owner_id=c.owner_id")
	                ->where($map)->count('distinct c.car_id');
	    // echo M()->getLastSql(),'<br />';
	    return $count;
    }
    /**
     * 计算个体成交金额
     * @param  string $where 查询字段
     * @param  string $map 查询条件
     * @param  string $paramer 查询条件
     * @return [type]      [description]
     */
    public function dealSuccess($where,$map,$paramer){
        // $user_m = new AccountManage();
        // $map1 = $user_m->getFourMap();
        // $map = array_merge($map,$map1);
        //dump($map);
        $map1 = $this->getParams1($paramer);
        $map1['c.suggest_max_price'] = array('neq',' ');
        $map1['c.suggest_min_price'] = array('neq',' ');
        $map = array_merge($map,$map1);

        // $map['o.status'] = array('eq',13);
    	$cols = "sum($where) as money";
    	$sum = M('cars')->alias('c')->field($cols)
    					->join("LEFT JOIN (SELECT * from (SELECT * from au_order ORDER BY bidding_end_time desc ) as od GROUP BY od.car_id ) o ON c.car_id=o.car_id")
		                ->join("LEFT JOIN ".C('DB_PREFIX')."car_owner as co ON co.owner_id=c.owner_id")
                        ->where($map)
                         // ->getField(true)
                        ->find();
        // dump(M()->getLastSql());
        return $sum;
    }
    /**
     * 获取集团统计合计信息
     * @return [type] [description]
     */
    public function familyCount($params){
    	// $info = $this->statInfo();
    	$sum_refer = array();
    	$sum_success = array();
    	$sum_money = array();        
    	foreach($params as $v){
    		$sum_refer[] = $v['refer'];
    		$sum_success[] = $v['success_count'];
    		$sum_money[] = $v['money'];            
    	}
    	$refer_sum = array_sum($sum_refer);
    	$success_sum = array_sum($sum_success);
    	$money_sum = array_sum($sum_money);        
    	$result[] = array(
    			'refer_sum' => $refer_sum,
    			'success_sum' => $success_sum,
    			'money_sum' => $money_sum
    		);
    	return $result;
    }
    /**
     * 获取统计信息
     */
    public function statInfo2($paramer){
        $user_m = new AccountManage();
        $ids = $user_m->returnIds();        
        if(is_array($ids)){
            foreach($ids as $k=>$value){                
                $result[] = $this->prepareInfo($paramer,$value,$k);                
            }                                    
            return $result;
        } else{
            $result = $this->prepareInfo($paramer,$ids);
            return $result;
        }
    }
    /**
     * 准备统一信息
     * @param string $paramer 时间参数
     * @param maxed $ids 4s店来源id
     */
    public function prepareInfo($paramer,$ids,$k=0){
        //成交金额
        $map3['c.car_status'] = array('eq',9);
        $map3['co.comefrom'] = array('eq',$ids);
        $money = $this->dealSuccess('c.success_price',$map3,$paramer);

        //参拍量
        //1,待预约检测 2,预约检测跟踪中 3,预约检测失败 
        $map1 = $this->getParams2($paramer);
        $map1['co.business_status'] = array('notin',array(1,2,3));
        $map1['co.comefrom'] = array('eq',$ids);
        $refer_count = $this->counts($map1);
        //成交量
        $map2 = $this->getParams1($paramer);
        $map2['c.car_status'] = array('eq',9);
        $map2['co.comefrom'] = array('eq',$ids);
        $success_count = $this->counts($map2);        
        //获取4s店的名称
        $u_type = $this->user_info;
        if($u_type['group_id'] == 5){
            $user_m = new AccountManage();
            $fours_name = $user_m->returnFoursIds();
            // var_dump($fours_name);
            $fourName = $fours_name[$k];
        }elseif($u_type['group_id'] == 4){
            $map['u_id'] = array('eq',$u_type['uid']);
            $fid = M('fours_user')->field('f_id')->where($map)->find();
            $fourN = M('fours')->where('fid='.$fid['f_id'])->find();
            $fourName = $fourN['f_name'];
        }
        $result[] = array(
                'refer' => $refer_count,
                'success_count' => $success_count,
                'rate' => round(($success_count/$refer_count)*100) .'%',
                'money' => intval($money['money']),
                'fourName' => $fourName
            );
         //dump($result);
        return $result;
    }
    /**
     * 获取统计信息
     */
    public function Preson($paramer){   
        $user_m = new AccountManage();
        $ids = $user_m->returnIds();
    	$map = array();
    	// $date_limit = $this->getParams($paramer);
        //成交金额
        $map3['c.car_status'] = array('eq',9);
        $map3['co.comefrom'] = array('eq',$ids);
        $money = $this->dealSuccess('c.success_price',$map3,$paramer);
        //参拍量
        //1,待预约检测 2,预约检测跟踪中 3,预约检测失败 
        $map1 = $this->getParams2($paramer);
        $map1['co.comefrom'] = array('eq',$ids);
        $map1['co.business_status'] = array('notin',array(1,2,3));
        $refer_count = $this->counts($map1);
        //成交量
        $map2 = $this->getParams1($paramer);
        $map2['c.car_status'] = array('eq',9);
        $map2['co.comefrom'] = array('eq',$ids);
        $success_count = $this->counts($map2);
        $result = array(
        		array('refer_sum' => $refer_count,
        		'success_sum' => $success_count,
        		'rate' => round(($success_count/$refer_count)*100) .'%',
        		'money_sum' => intval($money['money']))
        	);
        //4s店登录
        if(session('4s_login')){
        	//二维数组
        	$list = array(
        			array()
        		);
        }
        //集团登录
        $stat_result = array();
        if(session('family_login')){



        	//获取集团下对应的4s店ID  4s_ids
        	foreach($ids as $k=>$v){
        		//参拍量
		        $map1['co.business_status'] = array('notin',array(1,2,3));
		        $refer_count = $this->counts($map1);
		        //成交量
		        $map2['c.car_status'] = array('eq',9);
		        $success_count = $this->counts($map2);
		        //成交金额
		        $map3['c.car_status'] = array('eq',9);
		        $money = $this->dealSuccess();
        		$stat_result = array(
        				'refer' => $refer_count,
		        		'success_count' => $success_count,
		        		'rate' => round(($success_count/$refer_count)*100) .'%',
		        		'money' => $money['money']
        			);
        	}
        }
        return $result;
    }
    /**
     * 成交时间
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function getParams1($paramer){
    	//车辆检测时间
        if(!empty($paramer['start_time']) || !empty($paramer['end_time'])){
            if (empty($paramer['start_time'])) {
                $map['o.trade_time'] = array("elt", $paramer['end_time'].' 23:59:59');
            } elseif (empty($paramer['end_time'])) {
                $map['o.trade_time'] = array("egt", $paramer['start_time'] .' 00:00:00');
            } else {
                $map['o.trade_time'] = array(array("egt", $paramer['start_time'] .' 00:00:00'), array("elt", $paramer['end_time'] .' 23:59:59'));
            }
        }
        //参拍时间
        // if(!empty($paramer['start_time']) || !empty($paramer['end_time'])){
        //     if (empty($paramer['start_time'])) {
        //         $map['co.check_date'] = array("elt", $paramer['end_time'].' 23:59:59');
        //     } elseif (empty($paramer['end_time'])) {
        //         $map['co.check_date'] = array("egt", $paramer['start_time'] .' 00:00:00');
        //     } else {
        //         $map['co.check_date'] = array(array("egt", $paramer['start_time'] .' 00:00:00'), array("elt", $paramer['end_time'] .' 23:59:59'));
        //     }
        // }
        return $map;
    }
     /**
     * 参拍时间
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function getParams2($paramer){
        //车辆检测时间
        if(!empty($paramer['start_time']) || !empty($paramer['end_time'])){
            if (empty($paramer['start_time'])) {
                $map['co.check_date'] = array("elt", $paramer['end_time'].' 23:59:59');
            } elseif (empty($paramer['end_time'])) {
                $map['co.check_date'] = array("egt", $paramer['start_time'] .' 00:00:00');
            } else {
                $map['co.check_date'] = array(array("egt", $paramer['start_time'] .' 00:00:00'), array("elt", $paramer['end_time'] .' 23:59:59'));
            }
        }
        return $map;
    }
}